package concurrency.multithreading;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author xiaoming
 * @version 1.0
 * @date 2019-03-14 15:46
 **/
public class UserThreadPool {
    public static void main(String[] args) {

        // 缓存队列设置固定长度为2 (为了快速触发rejectHandler
        BlockingQueue queue = new LinkedBlockingQueue(2);

        // 假设外部任务线程来源机房1和机房2
        UserThreadFactory f1 = new UserThreadFactory("第1机房");
        UserThreadFactory f2 = new UserThreadFactory("第2机房");

        UserRejectHandeler handeler = new UserRejectHandeler();

        // 核心线程为1, 最大线程2 ( 保证rejectHandler触发
        ThreadPoolExecutor threadPoolFirst
                = new ThreadPoolExecutor(1, 2, 60, TimeUnit.SECONDS, queue, f1, handeler);
        ThreadPoolExecutor threadPoolSecond
                = new ThreadPoolExecutor(1, 2, 60, TimeUnit.SECONDS, queue, f2, handeler);

        // 创建400个线程任务
        Runnable task = new Task();
        for (int i = 0; i < 200; i++) {
            threadPoolFirst.execute(task);
            threadPoolSecond.execute(task);

        }
    }
}
